﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1.加密
{
    public class EncryptHelp
    {
        public static string DesEncrypt(string pToEncrypt, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] input = Encoding.Default.GetBytes(pToEncrypt);
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(input, 0, input.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}",b);
            }
            return ret.ToString();
        }

        ///MD5解密 方法类
        public static string DesDecrypt(string pToDecrypt, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < pToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }

            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            StringBuilder ret = new StringBuilder();

            return System.Text.Encoding.Default.GetString(ms.ToArray());
        }


        public static string Md52(string str)
        {
            string result = string.Empty;

            byte[] sor = Encoding.UTF8.GetBytes(str);
            MD5 md5 = MD5.Create();
            byte[] md5result = md5.ComputeHash(sor);
            StringBuilder SB = new StringBuilder(40);
            for (int i = 0; i < md5result.Length; i++)
            {
                SB.Append(md5result[i].ToString(("x2")));//"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
            }
            return SB.ToString();
        }

        public static string Md5(string str)
        {
            var buffer = Encoding.UTF8.GetBytes(str);

            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bytes = md5.ComputeHash(buffer);

            var sb = new StringBuilder();
            foreach (var t in bytes)
            {
                sb.Append(t.ToString("X2"));
            }
            return sb.ToString().ToLower();
        }

        /// <summary>
        /// Sha1加密
        /// </summary>
        /// <param name="str">要加密的字符串</param>
        /// <returns>加密后的十六进制的哈希散列（40个字符）</returns>
        public static string Sha1(string str)
        {
            var buffer = Encoding.UTF8.GetBytes(str);

            var sha1 = SHA1.Create();
            var bytes = sha1.ComputeHash(buffer);

            var stringBuilder = new StringBuilder();
            foreach (var byt in bytes)
            {
                stringBuilder.Append(byt.ToString("X2"));
            }

            return stringBuilder.ToString().ToLower();
        }

        public static void Test()
        {
            string password = "王八蛋";
            Console.WriteLine(Md5(password));
            Console.WriteLine(Md52(password));
            Console.WriteLine(Sha1(password));


            string key = "JUNDAOXT";//必須为8位

            byte[] Key = ASCIIEncoding.ASCII.GetBytes(key);

            string Password = EncryptHelp.DesEncrypt(password, key); //加密 后面的参数是密钥

            string JPassword = EncryptHelp.DesDecrypt(Password, key); //解密 后面的参数是密钥
        }
    }
}
